package com.tanhua.dubbo.api;

import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author SunSun
 * @date 2021/10/28
 */
@DubboService
public class CommentApiImpl implements CommentApi {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Comment> findComments(String movementId, CommentType comment, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId)).and("commentType")
                .is(comment.getType());
        Query query = Query.query(criteria).skip((page - 1) * pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));

        return mongoTemplate.find(query, Comment.class);
    }

    @Override
    public Integer save(Comment comment1) {
        Movement movement = mongoTemplate.findById(comment1.getPublishId(), Movement.class);
        if (movement != null) {
            comment1.setPublishUserId(movement.getUserId());
        }
            mongoTemplate.save(comment1);

            Query query = Query.query(Criteria.where("id").is(comment1.getPublishId()));
            Update update = new Update();
            if (comment1.getCommentType() == CommentType.COMMENT.getType()) {
                update.inc("commentCount", 1);
            } else if (comment1.getCommentType() == CommentType.LIKE.getType()) {
                update.inc("likeCount", 1);
            } else {
                update.inc("loveCount", 1);
            }
            FindAndModifyOptions options = new FindAndModifyOptions();
            options.returnNew(true);
            Movement comment = mongoTemplate.findAndModify(query, update, options, Movement.class);

            return comment.statisCount(comment1.getCommentType());
        }


    @Override
    public boolean likecomment(Long userId, String ID, CommentType like) {
        Criteria criteria = Criteria.where("userId").is(userId)
                .and("publishId").is(new ObjectId(ID))
                .and("commentType").is(like.getType());

        Query query = Query.query(criteria);

        return mongoTemplate.exists(query, Comment.class);
    }

    @Override
    public Integer delete(Comment comment) {
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        mongoTemplate.remove(Query.query(criteria), Comment.class);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Movement comment1 = mongoTemplate.findAndModify(query, update, options, Movement.class);

        return comment1.statisCount(comment.getCommentType());
    }

    @Override
    public Integer save1(Comment comment) {

        Comment movement = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        if (movement != null) {
            comment.setPublishUserId(movement.getUserId());
        }
        mongoTemplate.save(comment);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();

        if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else {
            update.inc("loveCount", 1);
        }

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment1 = mongoTemplate.findAndModify(query, update, options, Comment.class);

        return comment1.getLikeCount();
    }

    @Override
    public Integer delete1(Comment comment) {
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        mongoTemplate.remove(Query.query(criteria), Comment.class);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Comment comment1 = mongoTemplate.findAndModify(query, update, options, Comment.class);

        return comment1.getLikeCount();
    }

    @Override
    public List<Comment> likes(Integer page, Integer pagesize, Long userId, CommentType like) {
        Criteria criteria=Criteria.where("publishUserId").is(userId).and("commentType").is(like.getType());
        Query query=Query.query(criteria).skip((page-1)*pagesize).limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query,Comment.class);
    }

    @Override
    public Integer saveVideo(Comment comment) {
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        if (video != null) {
            comment.setPublishUserId(video.getUserId());
        }
        mongoTemplate.save(comment);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", 1);
        } else if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", 1);
        } else {
            update.inc("loveCount", 1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video comment1 = mongoTemplate.findAndModify(query, update, options, Video.class);

        return comment1.statisCount(comment.getCommentType());
    }

    @Override
    public Integer deleteVideo(Comment comment) {
        Criteria criteria = Criteria.where("userId").is(comment.getUserId())
                .and("publishId").is(comment.getPublishId())
                .and("commentType").is(comment.getCommentType());
        mongoTemplate.remove(Query.query(criteria), Comment.class);

        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));
        Update update = new Update();
        if (comment.getCommentType() == CommentType.COMMENT.getType()) {
            update.inc("commentCount", -1);
        } else if (comment.getCommentType() == CommentType.LIKE.getType()) {
            update.inc("likeCount", -1);
        } else {
            update.inc("loveCount", -1);
        }
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        Video comment1 = mongoTemplate.findAndModify(query, update, options, Video.class);

        return comment1.statisCount(comment.getCommentType());
    }

}

